package com.jzsm.common.utils;

import java.math.BigDecimal;

/**
 * BigDecimal工具类
 *
 * @author ruoyi
 */
public class DecimalUtils {

    public static boolean isNanOrZero(BigDecimal dec) {
        if (dec == null || dec.compareTo(BigDecimal.ZERO) == 0) {
            return true;
        }
        else {
            return false;
        }
    }

    public static Integer add(Integer dec1, Integer dec2) {
        return (dec1 == null ? 0 : dec1) + (dec2 == null ? 0 : dec2);
    }

    public static Integer compareTo(BigDecimal dec1, BigDecimal dec2) {
        return (dec1 == null ? BigDecimal.ZERO : dec1).compareTo(dec2 == null ? BigDecimal.ZERO : dec2);
    }

    public static BigDecimal add(BigDecimal... decs) {
        BigDecimal result = BigDecimal.ZERO;
        for (BigDecimal dec : decs) {
            result = result.add((dec == null ? BigDecimal.ZERO : dec));
        }
        return result;
    }

    public static BigDecimal subtract(BigDecimal... decs) {
        BigDecimal result = decs[0]==null ? BigDecimal.ZERO:decs[0];
        for (int i=1;i<decs.length;i++) {
            result = result.subtract(
                    decs[i]==null ? BigDecimal.ZERO:decs[i]
            );
        }
        return result;
    }

    public static BigDecimal multiplyIfnullZero(BigDecimal... decs) {
        BigDecimal result = BigDecimal.ONE;
        for (BigDecimal dec : decs) {
            if(dec == null){
                result = result.multiply(BigDecimal.ZERO);
            }
            else if (dec.compareTo(BigDecimal.ZERO) == 0) {
                return BigDecimal.ZERO;
            }
            else {
                result = result.multiply(dec);
            }
        }
        return result;
    }

   public static BigDecimal multiplyIfnullOne(BigDecimal... decs) {
       return multiply(decs);
    }
    public static BigDecimal multiply(BigDecimal... decs) {
        BigDecimal result = BigDecimal.ONE;
        for (BigDecimal dec : decs) {
            if(dec == null){
                result = result.multiply(BigDecimal.ONE);
            }
            else if (dec.compareTo(BigDecimal.ZERO) == 0) {
                return BigDecimal.ZERO;
            }
            else {
                result = result.multiply(dec);
            }
        }
        return result;
    }

    public static BigDecimal divide(BigDecimal dec1, BigDecimal dec2, int scale, int round) {
        if (dec1 == null || dec2 == null || dec1.compareTo(BigDecimal.ZERO) == 0 || dec2.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO;
        }
        return dec1.divide(dec2, scale, round);
    }
}
